package zjc.bookkeeping.tools;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

import zjc.bookkeeping.R;
import zjc.bookkeeping.adapter.ConsumptionNameAdapter;
import zjc.bookkeeping.db.DBManager;

public class SelectClassDialog extends Dialog implements View.OnClickListener {
    ListView classLv;
    TextView allTv,outcomeTv,incomeTv;
    List<String> list;
    ConsumptionNameAdapter adapter;
    String selectName = "全部";
    boolean allFlag = true;

    public interface OnSelectClassListener{
        public void onSelectClassSure(String name);
    }
    OnSelectClassListener onSelectClassListener;

    public void setOnSelectClassListener(OnSelectClassListener onSelectClassListener) {
        this.onSelectClassListener = onSelectClassListener;
    }

    public SelectClassDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_select_class);
        initView();
        classLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                TextView textView = view.findViewById(R.id.dialog_select_class_item_tv);
                selectName = textView.getText().toString();
                onSelectClassListener.onSelectClassSure(selectName);
                cancel();
            }
        });
    }


    private void initView() {
        allTv = findViewById(R.id.dialog_select_class);
        outcomeTv = findViewById(R.id.dialog_select_class_outcome);
        incomeTv = findViewById(R.id.dialog_select_class_income);
        classLv = findViewById(R.id.dialog_select_class_lv);
        getAllAdapter();

        allTv.setOnClickListener(this);
        outcomeTv.setOnClickListener(this);
        incomeTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.dialog_select_class:
                if (allFlag){
                    setupColor();
                    allTv.setBackgroundColor(Color.parseColor("#729FCF"));
                    getAllAdapter();
                }
                break;
            case R.id.dialog_select_class_outcome:
                setupColor();
                outcomeTv.setBackgroundColor(Color.parseColor("#729FCF"));
                getOutcomeAdapter();
                break;
            case R.id.dialog_select_class_income:
                setupColor();
                incomeTv.setBackgroundColor(Color.parseColor("#729FCF"));
                getIncomeAdapter();
                break;
        }
    }
    private void getAllAdapter() {
        list = new ArrayList<>();
        list.add("全部");
        list.add("支出");
        list.add("收入");
        list.add("转账");
        adapter = new ConsumptionNameAdapter(getContext(),list);
        adapter.notifyDataSetChanged();
        classLv.setAdapter(adapter);
    }
    private void getIncomeAdapter() {
        list = new ArrayList<>();
        List<String> nameList = DBManager.getConsumptionClassName(1);
        list.addAll(nameList);
        adapter = new ConsumptionNameAdapter(getContext(),list);
        adapter.notifyDataSetChanged();
        classLv.setAdapter(adapter);
    }

    private void getOutcomeAdapter() {
        list = new ArrayList<>();
        List<String> nameList = DBManager.getConsumptionClassName(0);
        list.addAll(nameList);
        adapter = new ConsumptionNameAdapter(getContext(),list);
        adapter.notifyDataSetChanged();
        classLv.setAdapter(adapter);
    }

    private void setupColor() {
        allTv.setBackgroundColor(Color.parseColor("#C5E0FB"));
        outcomeTv.setBackgroundColor(Color.parseColor("#C5E0FB"));
        incomeTv.setBackgroundColor(Color.parseColor("#C5E0FB"));
    }

    //    设置Dialog控件显示位置
    public void setClassDialogSize(){
        Window window =getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        Display display = window.getWindowManager().getDefaultDisplay();
        layoutParams.width = (int) (display.getWidth());
        layoutParams.gravity = Gravity.CENTER;
        window.setAttributes(layoutParams);
    }
    public void showNameList(String name){
        outcomeTv.setVisibility(View.GONE);
        incomeTv.setVisibility(View.GONE);
        allTv.setText(name);
        getNameAdapter(name);
        allFlag = false;
    }

    private void getNameAdapter(String name) {
        list = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        switch (name){
            case "账本":
                nameList = DBManager.getAllAccountName();
                break;
            case "支出":
                nameList = DBManager.getConsumptionClassName(0);
                break;
            case "收入":
                nameList = DBManager.getConsumptionClassName(1);
                break;
        }

        list.addAll(nameList);
        adapter = new ConsumptionNameAdapter(getContext(),list);
        adapter.notifyDataSetChanged();
        classLv.setAdapter(adapter);
    }
}
